//
//  MyOrderViewCell.m
//  XRmenu-HD
//
//  Created by shengchao yang on 11-12-14.
//  Copyright (c) 2011年 home user. All rights reserved.
//

#import "MyOrderViewCell.h"
#import "MaskView.h"
#import "XRTextInputView.h"


#define kTagOfAlter 6001

@implementation MyOrderViewCell
@synthesize celldata,delegate;

//初始化数据
-(void) setCelldata:(DishOrder *)oneOrder{
    celldata=oneOrder;
    
    //菜名
    if ([self viewWithTag:kTagOfStartLabel+1]) {
        [(UILabel*)[self viewWithTag:kTagOfStartLabel+1] setText:oneOrder.ddName];
    }
    
    //数量
    if ([self viewWithTag:kTagOfStartLabel+2]) {
        [(UILabel*)[self viewWithTag:kTagOfStartLabel+2] setText:[NSString stringWithFormat:@"%0.2f",[oneOrder.ddNumber floatValue]]];
    }
    
    //单价
    if ([self viewWithTag:kTagOfStartLabel+3]) {
        NSString* price  = [NSString stringWithFormat:@"%@%0.2f",[APPSESSION.allconfig getStringValueAtPath:@"System Config/currency"],[oneOrder.ddPrice floatValue]];
        if ([oneOrder.ddPrice floatValue]==0) {
            price=loc(@"时价");
        }
        [(UILabel*)[self viewWithTag:kTagOfStartLabel+3] setText:[NSString stringWithFormat:@"%@/%@",price,oneOrder.ddUnit]];
    }
    
    //备注(烧法)
    if ([self viewWithTag:kTagOfStartLabel+4]) {
        NSString *tempRemark=oneOrder.ddRemark;
        if ([tempRemark length]>=7) {
            tempRemark=[oneOrder.ddRemark substringToIndex:7];
        }
        [(UILabel*)[self viewWithTag:kTagOfStartLabel+4] setText:tempRemark]; 
    }
}

//序号
-(void) setID:(NSInteger) rowIndex{
    if ([self viewWithTag:kTagOfStartLabel]) {
        [(UILabel*)[self viewWithTag:kTagOfStartLabel] setText:[NSString stringWithFormat:@"%d",rowIndex]];
    }
}

#pragma mark 重载button处理方法
-(void) buttonClicked:(id)sender
{
	NSInteger bid = [sender tag];
    if (bid == kTagOfStartBT) {     
        DebugLog(@"点击了删除按钮");
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        UIAlertView* av =[[ UIAlertView alloc] initWithTitle:loc(@"删除确认") 
                                                     message:[NSString stringWithFormat:@"%@%@ ？",loc(@"确定要删除"),self.celldata.ddName] 
                                                    delegate:self 
                                           cancelButtonTitle:loc(@"取消") 
                                           otherButtonTitles:loc(@"确定"), nil];
        [av show];
        [av release];
	}
	if (bid == (kTagOfStartBT + 1)) {        
        DebugLog(@"点击了增加按钮");
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        XRmenuError *outErr=nil;
        [XRmenuCoreData updateDishOrderNum:self.celldata Increase:num(1) Error:&outErr];
        [self.delegate updateMyOrderData];//更新tableview里的数据
	}
    else if (bid == (kTagOfStartBT + 2)) {        
        DebugLog(@"点击了减少按钮");
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        XRmenuError *outErr=nil;
        [XRmenuCoreData updateDishOrderNum:self.celldata Increase:num(-1) Error:&outErr];
        [self.delegate updateMyOrderData];//更新tableview里的数据
	}
    else if (bid == (kTagOfStartBT + 3)) {        
        DebugLog(@"点击了备注按钮");
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
		NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:self.celldata, @"OrderData", nil];
		postNWithInfos(kMessage_RootView_PluNotesViewDisplay, self, info);
	}
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==alertView.cancelButtonIndex) {
        DebugLog(@"点击了取消按钮");
        return;
    }
    else{
        DebugLog(@"点击了确定按钮");
        XRmenuError *outErr=nil;
        [XRmenuCoreData deleteOneDishOrder:self.celldata Error:&outErr];
        [self.delegate updateMyOrderData];//更新tableview里的数据
    }
}
//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
    [super dealloc];
} 
@end
